
import axios from 'axios'

const API_BASE_URL = 'http://localhost:8089/api/projects'

export const fetchProjectsByCategory = async (categoryId) => {
    try {
        const res = await axios.get(`${API_BASE_URL}/${categoryId}`)
        let data = res.data

        // ✅ 添加图片路径映射逻辑
        return (data || []).map(item => ({
            id: item.id,
            name: item.name,
            image: item.image ? `http://localhost:8089/images/${item.image}` : '/default-avatar.jpg',
            category: item.category,
            level: item.level,
            brief: item.brief,
            region: item.region,
        }));
    } catch (error) {
        console.error(`获取分类 ${categoryId} 的项目失败:`, error)
        return []
    }
}

// 获取项目详情
export const fetchProjectDetail = async (categoryId, projectId) => {
    try {
        const res = await axios.get(`${API_BASE_URL}/${categoryId}/${projectId}`)
        let data = res.data

        // ✅ 统一添加图片路径
        return {
            ...data,
        }
    } catch (error) {
        console.error(`获取项目 ${projectId} 的详情失败:`, error)
        return null
    }
}

